<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <img src="../assets/logo.svg" alt="Logo" class="logo" />
          <h2>考研学习管理系统</h2>
        </div>
      </template>
      
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="0"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入学号/用户名"
            :prefix-icon="User"
            size="large"
            tabindex="1"
            @keyup.enter="$refs.passwordInput.focus()"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            ref="passwordInput"
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
            size="large"
            tabindex="2"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            size="large"
            tabindex="3"
            @click="handleLogin"
          >
            登录系统
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import axios from 'axios'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const formRef = ref(null)
const loading = ref(false)

const form = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    loading.value = true
    
    // 调用登录接口
    const response = await axios.post('/api/user/login', null, {
      params: {
        username: form.username,
        password: form.password
      }
    })
    
    const result = response.data
    console.log('登录响应:', result)
    
    // 判断业务状态码
    if (result.code === '200' || result.code === 200) {
      // 登录成功，保存用户信息
      localStorage.setItem('user', JSON.stringify(result.data))
      
      // 单独存储 userId，确保所有请求都能使用
      if (result.data && result.data.id) {
        localStorage.setItem('userId', result.data.id)
        console.log('已存储用户ID:', result.data.id)
      } else {
        console.warn('登录响应中没有找到用户ID')
      }
      
      ElMessage({
        message: '登录成功',
        type: 'success'
      })
      router.push('/')
    } else {
      // 显示错误消息
      console.log('错误信息:', result.message)
      ElMessage({
        message: result.message || '登录失败',
        type: 'error',
        duration: 3000,
        showClose: true
      })
    }
  } catch (error) {
    console.error('登录失败:', error)
    // 显示错误消息
    ElMessage({
      message: error.response?.data?.message || '登录失败，请稍后重试',
      type: 'error',
      duration: 3000,
      showClose: true
    })
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.login-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
  padding: 0;
  margin: 0;
  overflow: hidden;
  z-index: 1;
}

.login-card {
  width: min(90vw, 640px);
  max-width: 100%;
  border: none;
  border-radius: 16px;
  background-color: rgba(255, 255, 255, 0.95);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2),
              0 5px 15px rgba(0, 0, 0, 0.1);
  margin: 0;
  transform: translate3d(0, 0, 0) scale(1);
  backface-visibility: hidden;
  perspective: 1000px;
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  will-change: transform, box-shadow;
}

.login-card:hover {
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3),
              0 15px 25px rgba(0, 0, 0, 0.2);
  transform: translate3d(0, 0, 0) scale(1.02);
}

.card-header {
  text-align: center;
  padding: min(8vh, 56px) min(8vw, 64px);
  background: transparent;
  border-bottom: none;
}

.logo {
  width: min(15vw, 80px);
  height: min(15vw, 80px);
  margin-bottom: min(4vh, 24px);
  background-color: #1a2a6c;
  border-radius: 16px;
  padding: 16px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}

h2 {
  margin: 0;
  font-size: clamp(24px, 5vw, 36px);
  color: #1a2a6c;
  font-weight: 600;
  line-height: 1.2;
  background: linear-gradient(135deg, #1a2a6c, #b21f1f);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.login-form {
  padding: 0 min(8vw, 64px) min(8vh, 64px);
  width: 100%;
  box-sizing: border-box;
}

:deep(.el-input__wrapper) {
  background-color: #f8f9fa;
  box-shadow: 0 0 0 1px #e9ecef inset;
  border-radius: 10px;
  padding: 0 24px;
  height: min(8vh, 56px);
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #1a2a6c inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1a2a6c inset;
}

:deep(.el-input__inner) {
  height: 100%;
  font-size: clamp(14px, 2vw, 16px);
  color: #1a2a6c;
}

:deep(.el-input__inner::placeholder) {
  color: #6c757d;
}

:deep(.el-input__prefix-inner) {
  font-size: 20px;
  color: #1a2a6c;
}

.login-button {
  width: 100%;
  height: min(8vh, 56px);
  font-size: clamp(16px, 2vw, 18px);
  font-weight: 600;
  letter-spacing: 2px;
  background: linear-gradient(135deg, #1a2a6c, #b21f1f);
  border: none;
  border-radius: 10px;
  margin-top: 16px;
  transition: all 0.3s ease;
}

.login-button:hover {
  background: linear-gradient(135deg, #b21f1f, #1a2a6c);
  transform: translateY(-2px);
}

:deep(.el-form-item) {
  margin-bottom: min(6vh, 36px);
}

:deep(.el-form-item:last-child) {
  margin-bottom: 0;
}

:deep(.el-form-item__error) {
  color: #ef4444;
  font-size: 14px;
  padding-top: 6px;
}

/* 隐藏底部图标 */
:deep(.v-popper) {
  display: none !important;
}

:deep(.v-popper__popper) {
  display: none !important;
}

:deep([class*="popper"]) {
  display: none !important;
}

:deep([class*="tooltip"]) {
  display: none !important;
}

:deep(.el-card__header) {
  border-bottom: none;
  padding: 0;
}

:deep(.el-card) {
  border: none;
}

:deep(.el-card__body) {
  padding: 0;
  border-top: none;
}
</style> 